<template>
  <FModal width="800" @ok="ok" :loading="loading"
          ref="refFModal" :header="modalTitle">
    <div style="padding-right: 30px" v-loading="loading">
      <el-form ref="refForm" status-icon label-width="180px" :model="roleData">
        <el-form-item prop="roleName" label="角色名称" :rules="[required]">
          <el-input v-model="roleData.roleName" placeholder="请输入角色名称"/>
        </el-form-item>

        <el-form-item prop="roleCode" label="角色编码" :rules="[required]">
          <el-input v-model="roleData.roleCode" placeholder="请输入角色编码"/>
        </el-form-item>

        <el-form-item prop="roleName" label="排序号" :rules="[required]">
          <el-input-number style="width: 100%" v-model="roleData.sort" placeholder="请输入排序号"/>
        </el-form-item>

        <el-form-item prop="departRole" label="角色所属" :rules="[required]">
          <SystemDict :disabled="isNotEmpty(roleData.id)" dict-code="departRole"
                      v-model="roleData.departRole"/>
        </el-form-item>

        <el-form-item prop="roleDefault" label="设置默认" :rules="[required]">
          <SystemDict dict-code="booleanYesNot" v-model="roleData.roleDefault"/>
        </el-form-item>

        <el-form-item prop="roleStatus" label="状态" :rules="[required]">
          <SystemDict dict-code="roleStatus" v-model="roleData.roleStatus"/>
        </el-form-item>

        <el-form-item prop="showInUnitManagePage" label="单位/者部门管理页显示" :rules="[required]">
          <SystemDict dict-code="booleanYesNot" v-model="roleData.showInUnitManagePage"/>
        </el-form-item>

        <el-form-item prop="roleName" label="角色说明">
          <el-input type="textarea" v-model="roleData.roleDesc" placeholder="请输入角色说明"/>
        </el-form-item>
      </el-form>
    </div>
  </FModal>
</template>
<script setup>
import FModal from '@/components/FModal/index.vue';
import {computed, ref, shallowRef} from 'vue';
import {isEmpty, isNotEmpty, real, required} from '@/utils/CommonUtils';
import SystemDict from '@/components/SystemDict/index.vue';
import {getAction, postAction} from '@/request/BaseRequest';
import {ElMessage} from 'element-plus';

const emits = defineEmits(['saved']);
const refForm = shallowRef();
const refFModal = shallowRef();
const roleData = ref({});
const loading = shallowRef(false);
let unitData = undefined;

function setData(_unitData, _roleData) {
  unitData = _unitData;
  if (isEmpty(_roleData)) {
    //新建页面
    createDefaultValue();
  } else {
    //编辑页面
    loadRoleData(_roleData);
  }
  refFModal.value.open();
}

function loadRoleData({id}) {
  loading.value = true;
  getAction("/sys/role/loadRoleData", {id}).then(data => {
    loading.value = false;
    if (data.success) {
      roleData.value = data.result;
    }
  });
}

function createDefaultValue() {
  let data = {
    roleStatus: '0',
    departRole: false,
    roleDefault: false,
    showInUnitManagePage: false,
    systemPreset: false,
    roleType: 'custom',
  };
  data.unitId = real(unitData).id;
  loading.value = true;
  getAction('/sys/role/loadMaxRoleSort').then(data => {
    loading.value = false;
    if (data.success) {
      roleData.value.sort = data.result;
    }
  });
  roleData.value = data;
}

const modalTitle = computed(() => {
  if (isEmpty(real(roleData).id)) {
    return '新建角色';
  }
  return '编辑角色';
});

function ok() {
  refForm.value.validate((valid, errors) => {
    if (valid) {
      save();
    }
  });
}

const departRoleEditable = computed(() => {
  return isEmpty(real(roleData).id);
});

function save() {
  loading.value = true;
  let data = real(roleData);
  postAction('/sys/role/save', data).then(data => {
    loading.value = false;
    if (data.success) {
      emits('saved');
      refFModal.value.close();
      ElMessage({
        message: '保存成功',
        type: 'success',
      });
    }
  });
}

defineExpose({
  setData
});

</script>
<script>
export default {
  name: 'RoleEditModal',
};
</script>
<style scoped>

</style>